<?php
header('Access-Control-Allow-Origin: *');
require_once('Base.php');

$base = new Base();
$db = $base->getDB();

$name = $_POST["name"];
$key = $_POST["key"];

$cols = array("id", "name", "type", "gender", "status");
$db->where("loginName", $name);
$db->where("loginKey", $key);
$user = $db->getOne("users", $cols);

if ($user) {
	if ($user['status'] == 0) {
		echo $base->getResJson(false, null, LOGIN_UNCHECK, '用户登录失败，未通过审核');
		exit();
	}
	// unset($user["login_key"]);
	// unset($user["type"]);
	$token = $base->getTokenByUid($user['id']);
	if (empty($token)) {
		$token = md5('zxwx' . $user['id'] . microtime());
		$base->saveToken($token, $user['id']);
	}
	$login_info = array(
		"success" => true,
		"message" => "登录成功",
		"errCode" => 0,
		"data" => $user,
		"token" => $token
	);
	echo $base->getResJson(true, null, 0, '登录成功', $login_info);
} else {
	echo $base->getResJson(false, null, LOGIN_USER_ERROR, '用户登录失败，用户名或密码错误');
}
